<?php


namespace app\common\model;



class Teacher extends BaseModel
{
    protected $append = ['teaching_year'];
    protected $autoWriteTimestamp = 'int';
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    public function tag()
    {
        return $this->belongsToMany(Tag::class, 'teacher_tag', 'tag_id', 'teacher_id');
    }

    public function teacherCourse()
    {
        return $this->belongsTo(TeacherCourse::class, 'teacher_id', 'teacher_id');
    }

    public function experience()
    {
        return $this->hasMany(TeachingExperience::class)->order('starttime desc');
    }

    public static function getByField($whereField, $field)
    {
        return self::where($whereField, $field)->find();
    }

    public function course()
    {
        return $this->hasMany(Course::class, 'teacher_id', 'teacher_id')->where('status', 4);
    }

    public function images()
    {
        return $this->hasMany(TeacherImage::class, 'teacher_id', 'teacher_id');
    }

    /**
     * 教龄
     */
    public function getTeachingYearAttr($value, $data)
    {
        $teachingYear = 0;
        if (isset($data['year']) && $data['year']) {
            $year = date('Y') - $data['year'];
            $teachingYear = $year > 1 ? $year : 1;
        }
        return $teachingYear;
    }
}